<!DOCTYPE html>
<html>

<head>
  <title>修改新闻</title>
  {% include 'admin/common/header.html' %}
</head>

<body>
  <form class="layui-form" action="" lay-filter="myForm">
    <div class="mainBox">
      <div class="main-container">
        <div class="main-container">

          <input type="hidden" name="id" value="">

          <div class="layui-form-item">
              <label class="layui-form-label required">分类：</label>
              <div class="layui-input-block">
                  <select id="cat_id" name="cat_id" lay-search="" lay-verify="required" lay-filter="catIdOnchange">
                    <option value="">直接选择或搜索选择</option>
                  </select>
              </div>
          </div>

          <input type="hidden" id="cat_name" name="cat_name"/>

          <div class="layui-form-item">
            <label class="layui-form-label required">来源：</label>
            <div class="layui-input-block">
              <input type="text" name="source_name" lay-verify="required|source_name" autocomplete="off" placeholder="来源"
                class="layui-input">
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label required">标题：</label>
            <div class="layui-input-block">
              <input type="text" name="title" lay-verify="required|title" autocomplete="off" placeholder="标题"
                class="layui-input">
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label required">内容：</label>
            <div class="layui-input-block">
              <input type="hidden" id="content" name="content"/>
              <div id="newsContentEditor">
              </div>
            </div>
          </div>
          
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="button-container">
        <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
          lay-filter="news-update">
          <i class="layui-icon layui-icon-ok"></i>
          提交
        </button>
        <button type="reset" class="pear-btn pear-btn-sm">
          <i class="layui-icon layui-icon-refresh"></i>
          重置
        </button>
      </div>
    </div>
  </form>
  {% include 'admin/common/footer.html' %}
  <script type="text/javascript" src="/static/wangEditor/release/wangEditor.js"></script>
  <script type="text/javascript" src="/static/admin/admin/js/news.js"></script>
  <script>
    layui.use(['form', 'jquery'], function () {
      const form = layui.form;
      const $ = layui.jquery;

      let data = loadData('/admin/news/get_detail', {id: getUrlParam('id')}, "myForm");
      loadCategories('cat_id', data.cat_id);

      setEditorText();

      form.verify({
        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        source_name: function (value) {
          if (value.length > 10) {
            return '来源最多10个字符';
          }
        },
        title: function (value) {
          if (value.length > 30) {
            return '标题最多30个字符';
          }
        },
      });

      form.on('submit(news-update)', function (data) {
        $.ajax({
          url: '/admin/news/update',
          data: JSON.stringify(data.field),
          dataType: 'json',
          contentType: 'application/json',
          type: 'put',
          success: function (result) {
            if (result.success) {
              layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
                parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                parent.layui.table.reload('news-table')
                //parent.render()
              })
            } else {
              layer.msg(result.msg, { icon: 2, time: 1000 })
            }
          }
        })
        return false
      })
    })
  </script>
  <script>
  </script>
</body>

</html>